package top.hcode.hoj.dao.judge;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Lang;
import org.springframework.stereotype.Service;
import top.hcode.hoj.mapper.RemoteJudgeAccountMapper;
import top.hcode.hoj.pojo.entity.judge.RemoteJudgeAccount;

import java.util.Date;
import java.util.Map;

/**
 * @Author: Himit_ZH
 * @Date: 2021/5/18 17:46
 * @Description:
 */
@Service
public class RemoteJudgeAccountEntityService extends ServiceImpl<RemoteJudgeAccountMapper, RemoteJudgeAccount> {
    public void save(String oj, String uid, String username,
                     String password,
                     Map<String, String> cookies,
                     Date cookiesExpireAt) {
        QueryWrapper<RemoteJudgeAccount> query = new QueryWrapper<>();
        query.eq("oj", oj).eq("uid", uid);
        RemoteJudgeAccount dbo = getOne(query);
        if (dbo == null) {
            dbo = new RemoteJudgeAccount();
            dbo.setGmtCreate(new Date());
            dbo.setStatus(true);
            dbo.setVersion(0L);
        }

        dbo.setUid(uid);
        dbo.setUsername(username);
        dbo.setPassword(password);
        if (Lang.isNotEmpty(cookies)) {
            dbo.setCookiesJson(Json.toJson(cookies, JsonFormat.compact()));
            dbo.setGmtCookiesExpire(cookiesExpireAt);
        }
        dbo.setOj(oj);

        saveOrUpdate(dbo);
    }
}
